package com.stadium.config;

import com.fastsite.common.api.ApiTokenObjectStore;
import com.fastsite.secured.ApiSecuredHandlerInterceptor;
import com.fastsite.secured.ApiTokenArgumentResolver;
import com.fastsite.secured.ApiTokenValueExtractor;
import com.fastsite.swagger.config.SwaggerMvcConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class WebMvcConfig extends SwaggerMvcConfig {
    @Autowired
    private ApiTokenObjectStore store;

    @Autowired
    private ApiTokenValueExtractor extractor;

    public WebMvcConfig() {
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ApiSecuredHandlerInterceptor(store,extractor)).addPathPatterns("/web/**");
    }
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new ApiTokenArgumentResolver());
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("*.js").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        String path ="/opt/nginx/resources/";
        if(System.getProperty("os.name").startsWith("Windows")){
            path = "D:\\\\tmp\\file\\";
        }
        registry.addResourceHandler("/static/imgs/**").addResourceLocations("file:".concat(path));
    }
}
